package com.qf.service.impl;

import com.qf.service.DocumentService;
import com.qf.util.ElasticsearchUtil;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Map;

public class DocumentServiceImpl implements DocumentService {

    private RestHighLevelClient client = ElasticsearchUtil.client();


    @Override
    public void addDocument(String index, String type, String id, String source) throws IOException {
        //1. 创建request对象
        IndexRequest request = new IndexRequest(index, type, id);

        //2. 封装数据到request
        request.source(source, XContentType.JSON);

        //3. 通过client将request对象发送到es服务
        IndexResponse resp = client.index(request, RequestOptions.DEFAULT);

        //4. 根据返回结果输出
        if("CREATED".equalsIgnoreCase(resp.getResult().toString())){
            // 添加成功
            System.out.println("【文档操作】 添加文档成功！！！ index = " + index + "，type = " + type + "，id = " + id + "，source = " + source);
        }
    }

    @Override
    public void updateDocument(String index, String type, String id, Map doc) throws IOException {
        //1. 创建request对象
        UpdateRequest request = new UpdateRequest(index, type, id);

        //2. 封装数据到request
        request.doc(doc);

        //3. 通过client将request对象发送到es服务
        UpdateResponse resp = client.update(request, RequestOptions.DEFAULT);

        //4. 根据返回结果输出
        if("UPDATED".equalsIgnoreCase(resp.getResult().toString())){
            // 添加成功
            System.out.println("【文档操作】 修改文档成功！！！ index = " + index + "，type = " + type + "，id = " + id + "，doc = " + doc);
        }
    }

    @Override
    public void deleteDocumentById(String index, String type, String id) throws IOException {
        //1. 创建request对象
        DeleteRequest request = new DeleteRequest(index, type, id);

        //2. 封装数据到request

        //3. 通过client将request对象发送到es服务
        DeleteResponse resp = client.delete(request, RequestOptions.DEFAULT);

        //4. 根据返回结果输出
        if("DELETED".equalsIgnoreCase(resp.getResult().toString())){
            // 添加成功
            System.out.println("【文档操作】 删除文档成功！！！ index = " + index + "，type = " + type + "，id = " + id);
        }
    }

    @Override
    public void bulkDocument(BulkRequest request) throws IOException {
        //1. 调用
        BulkResponse resp = client.bulk(request, RequestOptions.DEFAULT);
        //2. 根据返回结果输出
        for (BulkItemResponse item : resp.getItems()) {
            System.out.println("操作的index：" + item.getIndex() + "，type：" + item.getType() + "，id：" + item.getId() + "操作结果：" + item.getFailure());
        }
    }
}
